﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeavenCommon.Enum;
using System.Text;
using FortuneHeavenCommon.ComMethod;
using FortuneHeavenCommon.DataMap;
using FortuneHeavenBAL;
using FortuneHeaven.Web.Common;

namespace FortuneHeaven.Web.Pages.Shop
{
    public partial class GameShowPurchase : BasePage
    {
        public int typeid;
        protected void Page_Load(object sender, EventArgs e)
        {
            SetPageHeader(FortuneHeavenCommon.Enum.MainPageTabType.shop);
            typeid =0;
            int.TryParse(Request["type"], out typeid);
            StringBuilder builder = new StringBuilder();
           
            this.usPager.PagerChanged += new Web.Controls.SePagination.PagerChangeHandler(usPager_PagerChanged);
            uclogin.LoginSuccess += new Web.Controls.LoginPanel.LoginSuccessHandler(uclogin_LoginSuccess);
            foreach (GameShowType type in Enum.GetValues(typeof(GameShowType)))
            {
                if (typeid == (int)type)
                {
                    builder.Append(string.Format("<a class=\"tl\" href=\"javascript://\">{0}</a>|", UtilMethod.GetDescription(type)));
                }
                else
                {
                    builder.Append(string.Format("<a  href=\"GameShowPurchase.aspx?type={0}\">{1}</a>|",(int)type, UtilMethod.GetDescription(type)));
                }
            }
            ltGameShowType.Text = builder.ToString().TrimEnd('|');
            if (!IsPostBack)
            {
                if (AuthorityCenter.CurrentUserID > 0)
                {
                    ShowUser();
                }
                else
                {
                    ShowLogin();
                }
                this.usPager.PageSize = 8;
                ResetList(typeid);
            }
        }

        void uclogin_LoginSuccess()
        {
         
            ShowUser();
        }

        private void ShowUser()
        {
            divHeadpic.Visible = true;
            uclogin.Visible = false;
            SetUserInfor();
        }
        private void ShowLogin()
        {
            divHeadpic.Visible = false;
            uclogin.Visible = true;
        }

        public void SetUserInfor()
        {
            ltUserName.Text = AuthorityCenter.CurrentUserName;
            UserInforBO bo = new UserInforBO();
            UserInfor infor = bo.GetUserInfor(AuthorityCenter.CurrentUserID);
            ltFortune.Text = infor.FortuneValue.ToString();
            if (!string.IsNullOrEmpty(infor.ShowImg))
            {
                imgHead.Src = GetPagesURL(FortuneHeavenCommon.ComMethod.CommonPath.GameShow, infor.ShowImg);
            }
        }

        void usPager_PagerChanged(object sender, int currentpage)
        {
            GameShowBO gift = new GameShowBO();
            rpGameShow.DataSource = gift.GetGameShowsByType(this.usPager.CurrentPageIndex, this.usPager.PageSize, typeid);
            rpGameShow.DataBind();
        }

        public void ResetList(int type)
        {
            GameShowBO gift = new GameShowBO();
            this.usPager.RecordCount = gift.GetTotalSizeByType(type);
            this.usPager.CurrentPageIndex = 1;
        }

        public void PurchaseHead(object sender, EventArgs e)
        {
            if (AuthorityCenter.CurrentUserID <= 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "infor", "alert('请先登陆!');", true);
                //Response.Write("<script> alert('请先登陆!');document.location ='" + Page.ResolveUrl("~/Login.aspx") + "'</script>");
            }
            else
            {
                LinkButton choose = (sender as LinkButton);
                string inputid = choose.CommandArgument;
                int headid = -1;
                int.TryParse(inputid, out headid);
                try
                {
                    if (headid >= 0)
                    {
                        UserInforBO bo = new UserInforBO();
                        UserInfor infor = bo.GetUserInfor(AuthorityCenter.CurrentUserID);
                        GameShowBO gameshowbo = new GameShowBO();
                        GameShow gameshow = gameshowbo.GetInfo(headid);
                        if (infor != null)
                        {
                            if (infor.FortuneValue >= gameshow.Price)
                            {
                                infor.FortuneValue = infor.FortuneValue - gameshow.Price;
                                infor.ShowImg = gameshow.Image;
                                bo.SaveUserInfor(infor);
                                GameShowOrdersBO ordersbo = new GameShowOrdersBO();
                                GameShowOrders order = new GameShowOrders();
                                order.GameShowID = gameshow.ID;
                                order.Name = gameshow.Name;
                                order.Description = gameshow.Description;
                                order.Time = DateTime.Now;
                                order.Type = gameshow.Type;
                                order.Price = gameshow.Price;
                                order.UserID = AuthorityCenter.CurrentUserID;
                                ordersbo.AddGameShowOrder(order);

                                GameScoreInfoBO gamebo = new GameScoreInfoBO();

                                GameScoreInfo score = gamebo.GetGameScore(order.UserID);
                                int giveScore = gameshow.Price * 50;
                                AccountsInfoBO acountbo = new AccountsInfoBO();
                                AccountsInfo acount = acountbo.GetAcount(AuthorityCenter.CurrentUserID);
                                if (score == null)
                                {
                                    score = new GameScoreInfo();
                                    score.Score = giveScore;
                                    score.Revenue = 0;
                                    score.InsureScore = 0;
                                    score.WinCount = 0;
                                    score.LostCount = 0;
                                    score.DrawCount = 0;
                                    score.FleeCount = 0;
                                    score.UserRight = 0;
                                    score.AllLogonTimes = 0;
                                    score.PlayTimeCount = 0;
                                    score.UserID = AuthorityCenter.CurrentUserID;
                                    score.OnLineTimeCount = 0;
                                    if (acount != null)
                                    {
                                        score.RegisterIP = acount.RegisterIP;
                                        score.RegisterDate = acount.RegisterDate;
                                        score.LastLogonDate = acount.LastLogonDate;
                                        score.LastLogonIP = acount.LastLogonIP;
                                    }
                                    else
                                    {
                                        score.RegisterIP = "1";
                                        score.RegisterDate = DateTime.Now;
                                        score.LastLogonDate = DateTime.Now;
                                        score.LastLogonIP = "1";
                                    }
                                    gamebo.AddGameScore(score);
                                }
                                else
                                {
                                    score.Score += giveScore;
                                    gamebo.SaveGameScore(score);
                                }

                                Response.Redirect("PurchaseShowSuccess.aspx?gamescore=" + giveScore);
                            }
                            else
                            {
                                ShowCurError("财富值不够！");
                            }
                        }
                        //GameShowOrdersBO game = new GameShowOrdersBO();

                    }
                }
                catch
                {
                    ShowCurError("服务器发生异常！");
                }
            }
        }
    }
}